package services.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.h2.util.StringUtils;

import play.libs.F.Promise;
import play.libs.WS;
import play.libs.WS.Response;
import services.BaseService;

public abstract class CoreService extends BaseService {
	
	public final static ObjectMapper mapper = new ObjectMapper();
	
	protected final static String baseUrl = "http://localhost:8080/services-0.0.1-SNAPSHOT";
	
	private static String buildRequest(String method, String...params){
		StringBuffer buffer = new StringBuffer(baseUrl);
		buffer.append(method);
		if(params != null){
			for(String param : params){
				buffer.append(StringUtils.urlEncode(param)).append("/");
			}
		}
		return buffer.toString();
	}

	public static Promise<WS.Response> get(String method, String... params ){
		return  get(buildRequest(method, params), new HashMap<String,String>());
	}

	public static <T> Promise<T> post(final Class<T> type, final String url, final Object body, String[] params, final Map<String,String> queryParams ){
		return post(type, buildRequest(url, params) , body, queryParams);
	}
	
	public static <T> Promise<T> put(final Class<T> type, final String url, final Object body, String[] params, final Map<String,String> queryParams ){
		return put(type, buildRequest(url, params) , body, queryParams);
	}
	
	public static <T> Promise<T> get(final Class<T> type, String method, String[] params, Map<String,String> queryParams){
		return  get(type, buildRequest(method, params), queryParams);
	}
	
	public static <T> Promise<T> get(final Class<T> type, String method, String... params ){
		return  get(type, buildRequest(method, params), new HashMap<String,String>());
	}
	
	public static <T> Promise<List<T>> getCollection(final Class<T> type, String method, String... params ){
		return  getCollection(type, buildRequest(method, params), new HashMap<String,String>());
	}
	
	public static <T,U> Promise<Map<T,U>> getMap(final Class<T> key, final Class<U> value, String method, String params ){
		return  getMap(key,value,buildRequest(method, params), new HashMap<String,String>());
	}
	
}
